<?php
/**
 * @version SVN: $Id: view.html.php 1149 2010-04-02 01:09:00Z elkuku $
 * @package    EasyCreator
 * @subpackage Views
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath (elkuku) {@link http://www.nik-it.de NiK-IT.de}
 * @author     Created on 07-Mar-2008
 * @license    GNU/GPL, see JROOT/LICENSE.php
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport( 'joomla.application.component.view');

class EasyCreatorViewZiper extends JView
{

    public function display($tpl = null)
    {
        $task = JRequest::getCmd('task');
        $this->ecr_project = JRequest::getCmd('ecr_project');

        //--Get the project
        $this->project = EasyProjectHelper::getProject($this->ecr_project);

        if( ! $this->project)
        {
            //--Something went wrong..
            JError::raiseWarning(100, JText::sprintf('Unable to load the project %s', $this->ecr_project));
            ecrHTML::easyFormEnd();

            return false;
        }

        //-- Draw h1 header
        ecrHTML::header(JText::_('Component ZIPer'), $this->project);

        $this->zip_dir_path = ECRPATH_BUILDS.DS.$this->ecr_project;

        if( ! is_dir($this->zip_dir_path) )
        {
            //--no zip dir - no zip files..
            $this->zip_dir_path = false;
        }

        if(in_array($task, get_class_methods($this)))
        {
            //--Execute the task
            $this->$task();
        }

        parent::display($tpl);

        ecrHTML::easyFormEnd();
    }// function

    private function ziper()
    {
        $this->setLayout('ziper');
    }//function

    private function ziperzip()
    {
        ecrLoadHelper('ziper');

        $this->buildopts = JRequest::getVar('buildopts',array());

        $this->EasyZiper = new EasyZIPer();

        $this->setLayout('ziperresult');
    }//function

    private function delete()
    {
        $this->setLayout('ziper');
    }// function

    /*
     * Drawing
     */
    protected function drawArchive()
    {
        echo '<h1>'.JText::_('Archive').'</h1>';
        ?>
		<script type="text/javascript">
		function deleteZipFile(name)
		{
		    $('row'+name).setStyle('display', 'none');
		}
		</script>
        <?php
        $this->zip_dir_path = ECRPATH_BUILDS.DS.$this->ecr_project;
        if( ! JFolder::exists($this->zip_dir_path))
        {
            ecrHTML::displayMessage(JText::_('Archive is empty'));

            $this->zip_dir_path = false;

            return;
        }

        $folders = JFolder::folders($this->zip_dir_path);
        $base_href = JURI::Root().'administrator/components/com_easycreator/builds/'.$this->ecr_project;

        rsort($folders);
        foreach ($folders as $folder)
        {
            echo '<div style="background-color: #B2CCE5; font-size: 1.3em; font-weight: bold; padding-left: 1em;">';
            echo $this->project->comName.' '.$folder;
            echo '</div>';

            $base_path = $this->zip_dir_path.DS.$folder;
            $files = JFolder::files( $base_path.DS );
            if( ! count( $files))
            {
                echo '<strong style="color: red;">'.JText::_('No ZIP files found').'</strong>';

                continue;
            }
            ?>
<table class="adminlist" cellspacing="5">
	<tbody>
		<tr style="background-color: #eee;">
			<th><?php echo JText::_('File'); ?></th>
			<th><?php echo JText::_('Modified'); ?></th>
			<th><?php echo JText::_('Size'); ?></th>
			<th colspan="2" align="center"><?php echo JText::_('Action'); ?></th>
		</tr>
		<?php
		$k = 0;
		foreach ($files as $file)
		{
		    $info = lstat($base_path.DS.$file);
		    $date = JFactory::getDate($info[9]);
		    $href = $base_href.'/'.$folder.'/'.$file;
		    $fsize = $info[7];

		    $js_delete = '';
		    $js_delete .= " document.adminForm.file_name.value='".$file."';";
		    $p = str_replace(JPATH_ROOT.DS, '', $base_path);
		    $p = str_replace('\\', '/', $p);
		    $js_delete .= " document.adminForm.file_path.value='".$p."';";
		    $js_delete .= " submitbutton('delete');";

		    $js_delete .= ' onclick="'.$js_delete.'"';

		    ?>
		<tr id="row<?php echo $file; ?>" class="<?php echo 'row'.$k; ?>">
			<td><?php echo $file; ?></td>
			<td><?php echo $date->toFormat(); ?></td>
			<td><?php echo ecrHTML::byte_convert($fsize); ?></td>
			<td width="2%"><a href="<?php echo $href; ?>"
				style="padding-left: 20px; height: 14px;"
				class="ecr_button img icon-16-save hasEasyTip"
				title="<?php echo JText::_('Download'); ?>::"> </a></td>
			<td width="2%">
			<div style="padding-left: 20px; height: 14px;"
				class="ecr_button img icon-16-delete hasEasyTip"
				title="<?php echo JText::_('Delete'); ?>::" <?php echo $js_delete; ?>>
			</div>
			</td>
		</tr>
		<?php
		$k = 1-$k;
		}//foreach
		?>
	</tbody>
</table>
		<?php
        }//foreach

    }//function

}//class
